﻿using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Web;
using Me.Amon.C;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.POS;
using Me.Amon.POS.M;
using Me.Amon.Util;
using Newtonsoft.Json;

namespace Me.Amon.POS
{
    /// <summary>
    /// 会员的操作对象
    /// </summary>
    public class Vip : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var obj = new MVip();

            // 公司
            var corp = context.Request[PPos.PARAM_KEY_CORP];
            if (string.IsNullOrEmpty(corp))
            {
                obj.error_code = PPos.ERROR_CODE_CORP_EMPTY;
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }

            // 操作参数
            var opt = context.Request[CObj.PARAM_KEY_OPT];
            if (string.IsNullOrEmpty(opt))
            {
                obj.error_code = PObj.ERROR_CODE_OPT_EMPTY;
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }

            // 操作数据
            var dat = context.Request[PPos.PARAM_KEY_DATA];
            if (string.IsNullOrWhiteSpace(dat))
            {
                obj.error_code = PPos.ERROR_CODE_DAT_EMPTY;
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }

            // 按操作类型返回数据
            switch (opt)
            {
                case PPos.PARAM_VALUE_OPT_SELECT:
                    SelectVip(corp, dat, context);
                    break;
                case PPos.PARAM_VALUE_OPT_CREATE:
                    break;
                case PPos.PARAM_VALUE_OPT_UPDATE:
                    break;
                case PPos.PARAM_VALUE_OPT_DELETE:
                    break;
                default:
                    obj.error_code = PObj.ERROR_CODE_OPT_ERROR;
                    context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                    break;
            }
            context.Response.End();
        }

        /// <summary>
        /// 查询符合条件的商品
        /// </summary>
        /// <param name="key"></param>
        /// <param name="context"></param>
        private void SelectVip(string corp, string key, HttpContext context)
        {
            var sPage = context.Request[PPos.PARAM_KEY_PAGE] ?? "";
            var iPage = 0;
            if (CharUtil.IsValidateLong(sPage))
            {
                iPage = int.Parse(sPage);
            }
            var sSize = context.Request[PPos.PARAM_KEY_PAGE_SIZE] ?? "";
            var iSize = 5;
            if (CharUtil.IsValidateLong(sSize))
            {
                iSize = int.Parse(sSize);
            }

            key = Regex.Replace(key, "[^\\w]+", "%");
            if (key[0] != '%')
            {
                key = '%' + key;
            }
            if (key[key.Length - 1] != '%')
            {
                key += '%';
            }

            // 数据库查询
            var dba = new RDBEngine();
            dba.AddTable(DVip.TABLE);
            dba.AddWhere(DVip.CORP_ID, corp);
            dba.AddWhere(DVip.CODE, "LIKE", key, true, true);
            dba.AddWhere(DVip.DISPLAY_NAME, "LIKE", key, true, true);
            dba.AddWhere(DVip.CELLPHONE, "LIKE", key, true, true);
            dba.AddWhere(DVip.TELEPHONE, "LIKE", key, true, true);

            // 计算要显示的数据区间
            var dt = dba.ExecuteSelect();
            var s = iPage * iSize;
            var t = s + iSize;
            var c = dt.Rows.Count;
            if (t > c)
            {
                t = c;
            }
            var vips = new MVip[t - s];
            c = 0;
            while (s < t)
            {
                vips[c++] = ReadVip(dt.Rows[s++]);
            }

            // 返回数据
            context.Response.Write(JsonConvert.SerializeObject(vips));
        }

        /// <summary>
        /// 读取一个商品对象
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private MVip ReadVip(DataRow row)
        {
            var vip = new MVip();
            vip.id = (string)row[DVip.ID];
            vip.code = (string)row[DVip.CODE];
            vip.last_name = (string)row[DVip.LAST_NAME];
            vip.first_name = (string)row[DVip.FIRST_NAME];
            vip.display_name = (string)row[DVip.DISPLAY_NAME];
            vip.sex = (int)row[DVip.SEX];
            vip.level = (int)row[DVip.LEVEL];
            vip.level_name = row[DVip.LEVEL_NAME] as string;
            vip.level_time = (DateTime)row[DVip.LEVEL_TIME];
            vip.point = (int)row[DVip.POINT];
            vip.birthday = (string)row[DVip.BIRTHDAY];

            return vip;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}